﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using jaysJourney_v01.Common;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using jaysJourney_v01.Utils;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace jaysJourney_v01.States
{
    class SettingsState : State
    {
        /**
         * MEMBER VARIABLES
         * */

        // Common
        private SpriteBatch mainSpriteBatch;
        private InputGroup menuButtonGroup;

        private Texture2D interfaceBG;

        // Buttons
        Button resMinus;
        Button resPlus;
        Button toggleFullScreen;
        Button volMinus;
        Button volPlus;
        Button backBtn;

        //screen params
        private int screenWidth;
        private int screenHeight;
        private int[,] screenSizes = new int[,]
            {{1024,768},
            {1280,720},
            {1280,800},
            {1680,1050},
            {1920,1080}};
        private int sizesCount = 1;

        // 

        public SettingsState(String name, StateManager stateManager) : base(name, stateManager) { }

        public override void initialize()
        {
            mainSpriteBatch = this.parentSpriteBatch;

            this.interfaceBG = this.getGame().Content.Load<Texture2D>(@"Textures\Interface\main_menu_v5");

            screenWidth = this.getGame().getGraphDevMan().PreferredBackBufferWidth;
            screenHeight = this.getGame().getGraphDevMan().PreferredBackBufferHeight;

            this.resMinus = new Button(this, "resolution-", @"Textures\Interface\minus_btn", @"Textures\Interface\minus_btn_ac", this.getGame().GraphicsDevice.Viewport.Width / 2 - 150, 350, Color.White, true, false);
            this.resPlus = new Button(this, "resolution+", @"Textures\Interface\plus_btn", @"Textures\Interface\plus_btn_ac", this.getGame().GraphicsDevice.Viewport.Width / 2 + 100, 350, Color.White, false, false);
            this.toggleFullScreen = new Button(this, "TOGGLE FULLSCREEN", @"Textures\Interface\menu_button", @"Textures\Interface\menu_button_Ac", 450, Color.White, false, true);
            this.volMinus = new Button(this, "volume-", @"Textures\Interface\minus_btn", @"Textures\Interface\minus_btn_ac", this.getGame().GraphicsDevice.Viewport.Width / 2 - 150, 550, Color.White, false, false);
            this.volPlus = new Button(this, "volume+", @"Textures\Interface\plus_btn", @"Textures\Interface\plus_btn_ac", this.getGame().GraphicsDevice.Viewport.Width / 2 + 100, 550, Color.White, false, false);
            this.backBtn = new Button(this, "BACK", @"Textures\Interface\back_btn", @"Textures\Interface\back_btn_ac", 20, this.getGame().GraphicsDevice.Viewport.Height - 74, Color.White, false, false);

            this.menuButtonGroup = new InputGroup(this, this.mainSpriteBatch);
            this.menuButtonGroup.addButton(resMinus);
            this.menuButtonGroup.addButton(resPlus);
            this.menuButtonGroup.addButton(toggleFullScreen);
            this.menuButtonGroup.addButton(volMinus);
            this.menuButtonGroup.addButton(volPlus);
            this.menuButtonGroup.addButton(backBtn);
        }

        public override void update(GameTime gameTime)
        {


            // update buttons
            Button click = this.menuButtonGroup.update(gameTime);

            // handle button clicks
                        
            if (click != null && click.getName() == "resolution-") 
            {
                if (sizesCount > 1)
                {
                    sizesCount--;
                    screenWidth = screenSizes[sizesCount - 1, 0];
                    screenHeight = screenSizes[sizesCount - 1, 1];
                    this.getGame().getGraphDevMan().PreferredBackBufferWidth = screenWidth;
                    this.getGame().getGraphDevMan().PreferredBackBufferHeight = screenHeight;
                    this.getGame().getGraphDevMan().ApplyChanges();
                    this.onEnterState();
                }
            }
            else if (click != null && click.getName() == "resolution+")
            {
                if (sizesCount < 5)
                {
                    sizesCount++;
                    screenWidth = screenSizes[sizesCount-1, 0];
                    screenHeight = screenSizes[sizesCount-1, 1];
                    this.getGame().getGraphDevMan().PreferredBackBufferWidth = screenWidth;
                    this.getGame().getGraphDevMan().PreferredBackBufferHeight = screenHeight;
                    this.getGame().getGraphDevMan().ApplyChanges();
                    this.onEnterState();
                }
                
            }
            else if (click != null && click.getName() == "TOGGLE FULLSCREEN")
            {
                if (this.getGame().getGraphDevMan().IsFullScreen)
                {
                    this.getGame().getGraphDevMan().PreferredBackBufferWidth = screenWidth;
                    this.getGame().getGraphDevMan().PreferredBackBufferHeight = screenHeight;                    
                }
                else
                {
                    this.getGame().getGraphDevMan().PreferredBackBufferWidth = this.getGame().GraphicsDevice.DisplayMode.Width; 
                    this.getGame().getGraphDevMan().PreferredBackBufferHeight = this.getGame().GraphicsDevice.DisplayMode.Height;
                }
                
                this.getGame().getGraphDevMan().ToggleFullScreen();
                this.onEnterState();
            }
            else if (click != null && click.getName() == "volume-")
            {
                if (this.getGame().volumeLevel >= 0.1) this.getGame().volumeLevel -= 0.1f;                
            }
            else if (click != null && click.getName() == "volume+")
            {
                if (this.getGame().volumeLevel <= 0.9) this.getGame().volumeLevel += 0.1f;
            }            
            else if (click != null && click.getName() == "BACK")
            {
                if(this.getStateManager().getLastActiveState() == "pauseState" ) this.getStateManager().setActiveState("pauseState");
                else this.getStateManager().setActiveState("menuState");
            }


            if (Keyboard.GetState().IsKeyDown(Keys.Escape)) this.getStateManager().setActiveState("pauseState");

        }

        public override void draw(GameTime gameTime)
        {
            // draw bg
            this.mainSpriteBatch.Draw(this.getGame().menuBg, new Rectangle(0, 0, this.getGame().GraphicsDevice.Viewport.Width, this.getGame().GraphicsDevice.Viewport.Height), new Rectangle(0, 0, this.getGame().menuBg.Width, this.getGame().menuBg.Height), Color.White);

            // draw interface bg
            this.mainSpriteBatch.Draw(this.interfaceBG, new Rectangle((this.getGame().GraphicsDevice.Viewport.Width / 2) - this.interfaceBG.Width / 2, 0, this.interfaceBG.Width, this.interfaceBG.Height), new Rectangle(0, 0, this.interfaceBG.Width, this.interfaceBG.Height), Color.White);

            // draw buttons
            this.menuButtonGroup.draw(gameTime);

            // draw txt
            this.mainSpriteBatch.DrawString(this.getGame().textFont, this.screenWidth + " x " + this.screenHeight, new Vector2(this.getGame().GraphicsDevice.Viewport.Width / 2-55, 350), Color.White);
            this.mainSpriteBatch.DrawString(this.getGame().textFont, "Volume: " + this.getGame().volumeLevel, new Vector2(this.getGame().GraphicsDevice.Viewport.Width / 2-60, 550), Color.White);
        }

        public override void onEnterState()
        {
            this.resMinus.setNewPosition(this.getGame().GraphicsDevice.Viewport.Width / 2 - 122, 350);
            this.resPlus.setNewPosition(this.getGame().GraphicsDevice.Viewport.Width / 2 + 72, 350);
            this.toggleFullScreen.setNewPosition(430);
            this.volMinus.setNewPosition(this.getGame().GraphicsDevice.Viewport.Width / 2 - 122, 550);
            this.volPlus.setNewPosition(this.getGame().GraphicsDevice.Viewport.Width / 2 + 72, 550);
            this.backBtn.setNewPosition(20, this.getGame().GraphicsDevice.Viewport.Height - 74);
        }
    }
}
